//
//  NearbyFriendsViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "NearbyFriendsViewController.h"
#import "NearbyFriendCell.h"
#import "ChatViewController.h"
#import "NearbyPhotos.h"
#import "PersonalInfomationViewController.h"

@interface NearbyFriendsViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 currentPage
 */
@property (nonatomic , assign) int currentPage;
/**
 longitude
 */
@property (nonatomic , assign) double longitude;
/**
 latitude
 */
@property (nonatomic , assign) double latitude;
@end

@implementation NearbyFriendsViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"附近豚友";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
}

- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArr removeAllObjects];
        self.currentPage = 1;
        [self dataRequest];
    }];
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.currentPage ++;
        [self dataRequest];
    }];
    self.myTableView.mj_footer.hidden = YES;

}

- (void)initData {
    self.currentPage = 1;
    //定位
    @weakify(self);
    [[LocationManager sharedManager] startLocationCompleteBlock:^(double latitude, double longitude) {
        @strongify(self);
        self.longitude = longitude;
        self.latitude = latitude;

        [self dataRequest];
    }];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NearbyFriendCell *cell = [NearbyFriendCell cellWithTableView:tableView];
    NearbyFriends *model = self.dataArr[indexPath.section];

    cell.model = model;
    //聊一聊  or  加好友
    @weakify(self);
    [cell setAddOrChatBlock:^(AddFriendOrChatType addFriendOrChatType) {
        @strongify(self);
        if (addFriendOrChatType == AddFriendOrChatTypeAdd) {
            //添加好友
            AddFriendVerifyViewController *vc = [[AddFriendVerifyViewController alloc] init];
            vc.identifier = model.memberNum;
            [self.navigationController pushViewController:vc animated:YES];
         } else if (addFriendOrChatType == AddFriendOrChatTypeChat) {
             [self beginChatWithIdentifier:model.name];
        }
    }];
    //去店铺
    [cell setGotoShopBlock:^{
        
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NearbyFriends *model = self.dataArr[indexPath.section];

    PersonalInfomationViewController *vc = [[PersonalInfomationViewController alloc] init];
    vc.friendId = model.memberId;
    vc.memberNum = model.memberNum;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - target action
- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - private method
- (void)dataRequest {
    
    NearbyFriendsParam *param = [[NearbyFriendsParam alloc] init];
    param.page = self.currentPage;
    param.limit = 15;
    double longitude = self.longitude ? self.longitude : 0;
    double latitude = self.longitude ? self.latitude : 0;
    
    param.longitude = [NSString stringWithFormat:@"%f",longitude];
    param.latitude = [NSString stringWithFormat:@"%f",latitude];
    param.memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    param.sex = @(0);//0是全部  1男 2女
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] nearbyFriendsWithParam:param] subscribeNext:^(NearbyFriendsList * _Nullable nearbyFriendsList) {
        
        [self.dataArr addObjectsFromArray:nearbyFriendsList.list];
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        if (self.currentPage == [nearbyFriendsList.totalPage intValue] || [nearbyFriendsList.totalPage intValue] == 0) {
            [self.myTableView.mj_footer endRefreshingWithNoMoreData];;
        } else {
            self.myTableView.mj_footer.hidden = NO;
        }
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}

- (void)beginChatWithIdentifier:(NSString *)identifier {
    TUIConversationCellData *data = [[TUIConversationCellData alloc] init];
    data.convId = identifier;
    data.convType = TIM_C2C;
    data.title = identifier;
    ChatViewController *chat = [[ChatViewController alloc] init];
    chat.conversation = data;
    [self.navigationController pushViewController:chat animated:YES];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
@end



